package com.yunhai.ucenter.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.common.dto.Ret;
import com.yunhai.common.dto.UserInfoEntity;
import com.yunhai.model.entity.ums.UmsPermission;
import com.yunhai.model.entity.ums.UmsUser;
import com.yunhai.model.vo.ums.AccountRemoveVo;
import com.yunhai.model.vo.ums.UmsUserVo;
import com.yunhai.ucenter.entity.vo.SysUserVo;
import com.yunhai.ucenter.service.SysUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.Permission;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author mengze
 * @since 2023-08-23
 */
@RestController
@RequestMapping("/sysUser")
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    @PostMapping("")
    public Ret addUser(@RequestBody UmsUserVo userVo) throws Exception {
        sysUserService.addUser(userVo);
        return Ret.ok();
    }

    @PutMapping("")
    public Ret updateUser(@RequestBody UmsUserVo userVo) throws Exception {
        sysUserService.updateUser(userVo);
        return Ret.ok();
    }


    @PutMapping("/password")
    public Ret updateUserPassword( @RequestBody UmsUser sysUser ) throws Exception {
        sysUserService.updateUserPassword(sysUser);
        return Ret.ok();
    }

    @PostMapping("/batchRemove")
    public Ret batchRemoveUser( @RequestBody List<Long> ids ) throws Exception {

        sysUserService.batchRemoveUser(ids);
        return Ret.ok();
    }


    @PostMapping("/account/remove")
    public Ret  removeAccount(@RequestBody AccountRemoveVo accountRemoveVo ) throws Exception {

        sysUserService.removeAccount(accountRemoveVo);
        return Ret.ok();
    }

    @GetMapping("/pagination")
    public Ret paginationUserList(PaginationVo paginationVo, SysUserVo sysUserVo) throws Exception {

        IPage page = sysUserService.paginationUserList(paginationVo, sysUserVo);
        return Ret.toPage(page);
    }

    @GetMapping("/allList")
    public Ret allUserList() throws Exception {

        List<UmsUser> sysUsers = sysUserService.allUserList();
        return Ret.ok(sysUsers);
    }

    @PostMapping("/listByIds")
    public Ret getUserListByIds( @RequestBody List<Long> userIds ) throws Exception {

        List<UmsUser> sysUsers = sysUserService.getUserListByIds(userIds);
        List<UserInfoEntity> userInfoDtos =  sysUsers.stream().map(item->{
            UserInfoEntity userInfoDto = new UserInfoEntity();
            userInfoDto.setId(item.getId());
            userInfoDto.setOpenId(item.getOpenid());
            userInfoDto.setUsername(item.getUserName());
            userInfoDto.setNickname(item.getNickName());
            return userInfoDto;
        }).collect(Collectors.toList());
        return Ret.ok(userInfoDtos);
    }




    @GetMapping("/userByOpenId")
    public Ret<UserInfoEntity> getUserByOpenId(@RequestParam("openId") String openId ) throws Exception {

        UmsUser sysUser = sysUserService.getUserByOpenId(openId);
        UserInfoEntity userInfoDto = new UserInfoEntity();
        userInfoDto.setId(sysUser.getId());
        userInfoDto.setUsername(sysUser.getUserName());
        userInfoDto.setNickname(sysUser.getNickName());
        return Ret.ok(userInfoDto);
    }


    @GetMapping("/curUserPermissionList")
    public Ret<List<UmsPermission>> curUserPermissionList() throws Exception {

        List<UmsPermission> sysMenus = sysUserService.curUserPermissionList();
        return Ret.ok(sysMenus);
    }


    @GetMapping("/monthUserCount")
    public Ret<Integer> getMonthCountOfUser() throws Exception {
        Integer monthCountOfUser = sysUserService.getMonthCountOfUser();
        return Ret.ok(monthCountOfUser);
    }


    @GetMapping("/getUserDetail")
    public Ret<UserInfoEntity> getUserInfo(@RequestParam("id") Long id  )  throws Exception {
        UmsUser sysUser = sysUserService.getUserInfo(id);
        UserInfoEntity userInfoDto = new UserInfoEntity();
        userInfoDto.setId(sysUser.getId());
        userInfoDto.setOpenId(sysUser.getOpenid());
        return Ret.ok(userInfoDto);
    }
}

